package com.graphsql.gamesdk.util;

import java.util.Random;

import android.content.Context;
import android.provider.Settings.Secure;
import android.telephony.TelephonyManager;

public class IMEIUtil {
	
	private static String imei = null;
	private static String android_id = null;

	public static String getImei(Context act) {
		if (imei != null) {
			return imei;
		}
		try {
			imei = ((TelephonyManager) act
					.getSystemService(Context.TELEPHONY_SERVICE)).getDeviceId();
			return imei;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";
	}
	
	public static void initImei(Context act) {
		getImei(act);
	}
	
	public static String getImei() {
		if (imei == null) {
			return "";
		}
		return imei;
	}
	
	public static String getAndroidId(Context act) {
		if(android_id!=null){
			return android_id;
		}
		try{
			android_id = "" +Secure.getString(act.getContentResolver(), Secure.ANDROID_ID);
			return android_id;
		}catch (Exception e) {
			e.printStackTrace();
		}
		return "";
	}
	
	public static String creatPushId(Context act){
		String id = "";
		id = IMEIUtil.getImei(act)+IMEIUtil.getAndroidId(act);
		id = MD5Util.md5(id);
		Random random=new Random(System.currentTimeMillis());
		id += ""+random.nextInt(1000000);
		return id;
	}
	
	public static String getSimId(Context act) {
		String simId="";
		try {
			TelephonyManager telMgr = (TelephonyManager) act.getSystemService(Context.TELEPHONY_SERVICE);
			simId = telMgr.getSimSerialNumber();
			if (simId == null) {
				simId = "";
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return simId;
	}
	
	public static String getDeviceIdAndroidId(Context act){
		String id = "";
		id = IMEIUtil.getImei(act)+"-"+IMEIUtil.getAndroidId(act);
		return id;
	}
}
